import pagination from '@/views/pagination'  //分页组件
import dialogForm from './view'
import agentHttp from '@/utils/agentHttp.js'
import {debounce} from '@/utils/index.js'
import exportExcel from '@/utils/exportExcel.js'
import {initDataTime,timestampToTime} from '@/utils/index.js' 
import {confirm} from '@/utils/confirm'      //二次确认组件
export default {
  name: 'allCarRources',
  created(){
      this.seachData(this.searchForm);
      // this.getStatusMap();
      
  },
  components:{dialogForm,pagination},
  data() {
  	return {
      //搜索字段
  		searchForm:{
  			carTypeName:"",  //汽车车型
        startDate:"",
        endDate:"",
        releasePerson:"",       //发布人
        status:"",               //车源状态
        dealerName:'',//经销商名称
        carSpecificationId:''  //车规
  		},
      statusMap:'',
      carSpecifications:'',//车规
      //表格字段
      tableData: [{}],
        //发送给弹窗的数据
      dialogSendData:{
        isAdd:true,                //是否为编辑或者新增  true是   false否
        dialogFormVisible:false,   //弹窗是否显示        true是   false否
        id:""          //请求参数            自定义 例如index或者id
      },
      formLabelWidth:'120px',
      resetFormStatus:false,
       startDateOpt: {
        disabledDate(time) {
          return time.getTime() > Date.now() 
        }
      },
      endDateOpt:{
        disabledDate:(time)=>{
          return  time.getTime()< new Date(this.searchForm.startDate).getTime();
        }
      },
  	}
  },

  methods: {

    seachData(){
      if(!this.resetFormStatus){
        // var params=this.searchForm||{};
        var params = initDataTime(this.searchForm)||{};

        this.tableData.data ? params.page = this.tableData.data.tableData.page : params.page = 1 ;
        this.tableData.data ? params.rows = this.tableData.data.tableData.rows : params.rows = 10;

        agentHttp.get('adminApi/admin/v_2.0/car_source/list',params).then((data)=>{
              this.tableData = data
              this.carSpecifications = data.data.carSpecifications ;
              this.statusMap = data.data.carScourceStatusMap;
        })
      }
    },
    debounceSeach(){
      this.tableData.page=1;
      debounce(this.seachData(this.searchForm),1000)
    },
    //重置
    resetSearchForm(formName) {
      this.resetFormStatus=true;
      this.tableData.page=1;
      this.tableData.rows=10;
      this.$refs[formName].resetFields();
      this.$nextTick(_ =>{
        this.resetFormStatus=false;
        this.seachData();
      })
    },
    //导出
    exportExcels(searchForm,url,name){
      exportExcel(searchForm,url,name)
    },
    //下架
    cancel(index,row){
            this.$nextTick(() => {
              var message = "下架";
               confirm(()=>{
                agentHttp.put(`adminApi/admin/v_2.0/car_source/down/${row.id}`).then((data)=>{
                  this.$message.success(data.data||"下架成功");
                  this.seachData();
                } )
              },()=>{
                this.$message.info("已取消"+message);
              },"确认下架该条数据吗?",message)
            })
    },

    /*表格操作按钮*/
    //查看
    handleSee(index, row){
      this.dialogSendData.isAdd=false;
      this.dialogSendData.id=row.id;
      this.dialogSendData.dialogFormVisible=true;
    },
    handleEdit(index, row){
      this.dialogSendData.isAdd=true;
      this.dialogSendData.id=row.id;
      this.dialogSendData.dialogFormVisible=true;
    },
      getStatusMap(){
      agentHttp.get('adminApi/admin/car_source/get_status_map' ).then((data)=>{
        this.statusMap = data.data.scourceStatus;
       
      })  
    },
    getDate(time){
      return timestampToTime(time);
    }
  }
}